<template>
  <div>
    <div class="bread">
      <div class="wrap">
        <nuxt-link to="/">
          <span>产品中心</span>
        </nuxt-link>
        <span class="arrow">＞</span>
        <nuxt-link
          :to="{
            name: 'list',
            query: {
              pname: pname,
              cname: cname,
              id: id
            }
          }"
        >
          {{ pname }}
        </nuxt-link>
        <span class="arrow">＞</span>
        <span class="active">{{ cname }}</span>
      </div>
    </div>

    <div class="category">
      <div class="item" v-for="(item, index) in attrList" :key="index">
        <div class="title">{{ item.name }}：</div>
        <div class="options">
          <div
            class="option"
            v-for="(oitem, oindex) in item.values"
            :key="oindex"
            :class="item.selected == oindex ? 'active' : ''"
            @click="switchCate(index, oindex)"
          >
            {{ oitem }}
          </div>
        </div>
      </div>
      <div class="item">
        <div class="title">应用场景：</div>
        <div class="options">
          <div
            class="option"
            v-for="(item, index) in scenarioList"
            :key="item.id"
            :class="scenarioListSelected == index ? 'active' : ''"
            @click="switchScenario(index)"
          >
            {{ item.dictLabel }}
          </div>
        </div>
      </div>
    </div>
    <div class="products">
      <div v-if="list.length > 0">
        <div class="head">
          <div class="title">
            {{ total }} 件商品 {{ ' ' }}
            <div class="active">{{ page }}</div>
            /{{ Math.ceil(total / limit) }}
          </div>

          <div class="switch">
            <div
              class="item pointer"
              @click="type = 0"
              :class="type == 0 ? 'active' : ''"
            >
              <img
                :src="
                  require(`@/assets/imgs/grid${
                    type == 0 ? '-active' : ''
                  }.png)`)
                "
              />
              矩阵
            </div>
            <div
              class="item pointer"
              @click="type = 1"
              :class="type == 1 ? 'active' : ''"
            >
              <img
                :src="
                  require(`@/assets/imgs/list${type == 1 ? '-active' : ''}.png`)
                "
              />
              列表
            </div>
          </div>
          <div class="clear" />
        </div>
        <div class="list" v-if="type == 0 && list.length > 0">
          <div class="item" v-for="(item, index) in list" :key="index">
            <div class="pic">
              <img
                :src="item.attrList[item.buyIndex].proPicList[0]"
                @click="
                  goto(
                    '../detail/detail?pid=' +
                      id +
                      '&pname=' +
                      encodeURIComponent(pname) +
                      '&cname=' +
                      encodeURIComponent(cname) +
                      '&id=' +
                      item.id
                  )
                "
                v-if="item.attrList.length > 0"
              />
              <img
                src="@/assets/imgs/nopic.png"
                @click="
                  goto(
                    '../detail/detail?pid=' +
                      id +
                      '&pname=' +
                      encodeURIComponent(pname) +
                      '&cname=' +
                      encodeURIComponent(cname) +
                      '&id=' +
                      item.id
                  )
                "
                v-else
              />
            </div>
            <div class="name">
              {{ item.name }}
            </div>
            <div class="price">
              {{
                item.attrList.length > 0
                  ? '$' + item.attrList[item.buyIndex].promotionPrice
                  : '暂无价格'
              }}
            </div>
            <div class="pic-list">
              <img
                :src="oitem.proPicList[0]"
                class="pic-item"
                v-for="(oitem, oindex) in item.attrList"
                :class="oindex == item.buyIndex ? 'active' : ''"
                :key="oindex"
                @click="switchAttr(index, oindex)"
              />

              <div
                class="pic-item dot"
                @click="
                  goto(
                    '../detail/detail?pid=' +
                      id +
                      '&pname=' +
                      encodeURIComponent(pname) +
                      '&cname=' +
                      encodeURIComponent(cname) +
                      '&id=' +
                      item.id
                  )
                "
                v-if="item.attrList.length > 6"
              >
                ···
              </div>
            </div>
            <div class="bottom">
              <div class="number">
                <div
                  class="minus"
                  :class="item.buyNumber == 1 ? 'disable' : ''"
                  @click="
                    list[index].buyNumber = Math.max(1, item.buyNumber - 1)
                    $forceUpdate()
                  "
                >
                  -
                </div>
                <input
                  type="number"
                  :value="item.buyNumber"
                  class="total"
                  @input="onInput($event, index)"
                />
                <div
                  class="plus"
                  @click="
                    list[index].buyNumber = item.buyNumber + 1
                    $forceUpdate()
                  "
                >
                  +
                </div>
              </div>
              <div
                class="button pointer"
                @click="
                  addToCart(
                    item.buyNumber,
                    item.attrList[item.buyIndex].id,
                    item.id
                  )
                "
              >
                加入购物车
              </div>
            </div>
          </div>
          <div class="clear" />
        </div>
        <div class="list2" v-if="type == 1">
          <div class="item" v-for="(item, index) in list" :key="index">
            <div class="pic">
              <img
                :src="item.attrList[item.buyIndex].proPicList[0]"
                @click="
                  goto(
                    '../detail/detail?pid=' +
                      id +
                      '&pname=' +
                      encodeURIComponent(pname) +
                      '&cname=' +
                      encodeURIComponent(cname) +
                      '&id=' +
                      item.id
                  )
                "
                v-if="item.attrList.length > 0"
              />
              <img
                src="@/assets/imgs/nopic.png"
                @click="
                  goto(
                    '../detail/detail?pid=' +
                      id +
                      '&pname=' +
                      encodeURIComponent(pname) +
                      '&cname=' +
                      encodeURIComponent(cname) +
                      '&id=' +
                      item.id
                  )
                "
                v-else
              />
            </div>
            <div class="name">{{ item.name }}</div>
            <div class="attrs" v-if="item.attrList.length > 0">
              <div
                class="attrs-item"
                v-for="(oitem, oindex) in item.attrList[item.buyIndex]
                  .productAttr"
                :key="oindex"
              >
                {{ oitem.name }}：{{ oitem.value }}
              </div>
            </div>
            <div class="pic-list">
              <img
                :src="oitem.proPicList[0]"
                class="pic-item"
                v-for="(oitem, oindex) in item.attrList"
                :class="oindex == item.buyIndex ? 'active' : ''"
                :key="oindex"
                @click="switchAttr(index, oindex)"
              />
            </div>
            <div class="price">
              {{
                item.attrList.length > 0
                  ? '$' + item.attrList[item.buyIndex].promotionPrice
                  : '暂无价格'
              }}
            </div>
            <div class="number">
              <div
                class="minus"
                :class="item.buyNumber == 1 ? 'disable' : ''"
                @click="
                  list[index].buyNumber = Math.max(1, item.buyNumber - 1)
                  $forceUpdate()
                "
              >
                -
              </div>
              <input
                type="number"
                :value="item.buyNumber"
                class="total"
                @input="onInput($event, index)"
              />
              <div
                class="plus"
                @click="
                  list[index].buyNumber = item.buyNumber + 1
                  $forceUpdate()
                "
              >
                +
              </div>
            </div>
            <div
              class="button pointer"
              @click="
                this.$parent.addToCart(
                  item.buyNumber,
                  item.attrList[item.buyIndex].id,
                  item.id
                )
              "
            >
              加入购物车
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  async asyncData({ $axios, store, route }) {
    let productCategoryTwoId = route.query.id || 26
    let id = route.query.id
    let pname = route.query.pname
    let cname = route.query.cname
    let scenarioList = []
    let scenarioListSelected = route.query.scenarioListSelected || -1
    let attrList = []
    let cartList = []
    let cartNum = 0
    let list = []
    let attrs = []
    let limit = route.query.limit || 10
    let page = route.query.page || 1
    let total = 0
    let scenariosIds = route.query.scenariosIds || []
    const attrsData = await $axios.get('productApi/getProductReAttrs', {
      id: productCategoryTwoId
    })
    attrList = attrsData.data

    const scenarioData = await $axios.get('productApi/scenariosList', {
      id: productCategoryTwoId
    })
    scenarioList = scenarioData.data

    const cardData = await $axios.get('productApi/getCartList', {
      id: productCategoryTwoId
    })
    cartList = cardData.data
    cartNum = cartList.length

    const listData = await $axios.post('productApi/getProductSeriesList', {
      scenariosIds,
      productCategoryTwoId,
      attrs,
      page,
      limit
    })
    list = listData.list
    total = listData.total

    return {
      productCategoryTwoId,
      id,
      pname,
      cname,
      attrList,
      scenarioList,
      scenarioListSelected,
      cartList,
      cartNum,
      scenariosIds,
      list,
      total,
      limit,
      page
    }
  },
  data() {
    return {
      type: 0,
      refresh: true,
      pname: '',
      cname: '',
      scenariosIds: []
    }
  },
  watchQuery: true,
  methods: {
    switchCate(index, oindex) {
      if (this.attrList[index].selected == oindex) {
        let productAttributeId = this.attrList[index].productAttributeId
        this.attrList[index].selected = -1
        this.attrs.forEach((item, index) => {
          if (item.productAttributeId == productAttributeId) {
            this.attrs.splice(index, 1)
          }
        })
        this.$forceUpdate()
      } else {
        this.attrList[index].selected = oindex
        let attrs = {
          productAttributeId: this.attrList[index].productAttributeId,
          values: [this.attrList[index].values[oindex]]
        }
        this.attrs.push(attrs)
      }
      this.$router.push({
        query: {
          ...this.$route.query,
          attrs: this.attrs
        }
      })
    },
    switchScenario(index) {
      if (this.scenarioListSelected == index) {
        this.scenarioListSelected = -1
        this.scenariosIds = []
      } else {
        this.scenarioListSelected = index
        this.scenariosIds.push(this.scenarioList[index].id)
      }

      this.$router.replace({
        query: {
          ...this.$route.query,
          scenariosIds: this.scenariosIds,
          scenarioListSelected: this.scenarioListSelected
        }
      })
    },

    onInput(e, index) {
      this.list[index].buyNumber = e.detail.value
    },
    switchAttr(index, oindex) {
      this.list[index].buyIndex = oindex
      this.$forceUpdate()
    },
    addToCart(num, productId, productSeriesId) {
      uni.request({
        url: this.apiUrl + 'productApi/addOrUpdateToCart',
        method: 'POST',
        header: {
          Authorization: this.getToken()
        },
        data: {
          num,
          productId,
          productSeriesId
        },
        success: res => {
          if (num == 0) {
            this.showToast('已删除')
          } else {
            this.showToast('已添加购物车')
          }
          this.getCartList()
        },
        complete: () => {}
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.head {
  height: 30px;
  line-height: 30px;

  .title {
    font-size: 20px;
    font-weight: 500;
    color: #222222;
    display: inline-block;

    .active {
      color: #ff590f;
      display: inline;
    }
  }

  .switch {
    float: right;

    .item {
      display: flex;
      height: 30px;
      align-items: center;
      float: left;
      width: 82px;
      background-color: #dddddd;
      font-size: 14px;
    }

    .item.active {
      background-color: #ff590f;
      color: #ffffff;
    }

    img {
      height: 14px;
      width: 14px;
      margin-left: 16px;
      margin-right: 8px;
    }
  }
}

.list {
  margin-top: 20px;
  margin-left: -8px;
  margin-right: -8px;

  .item:hover {
    border: 2px solid #ff590f;

    .bottom .button {
      background: #ff590f;
    }
  }

  .item {
    width: 288px;
    height: 484px;
    border: 1px solid #dddddd;
    float: left;
    margin-left: 8px;
    margin-right: 8px;
    margin-bottom: 16px;

    .pic {
      height: 248px;
      width: 248px;
      background-color: white;
      width: 248px;
      text-align: center;
      margin-top: 20px;
      margin-left: 20px;

      img {
        height: 248px;
        width: 248px;
      }
    }

    .name {
      width: 248px;
      height: 56px;
      font-size: 16px;
      font-weight: 600;
      color: #222222;
      line-height: 28px;
      margin-top: 20px;
      margin-left: 20px;
      text-align: center;
      line-height: 28px;
    }

    .price {
      font-size: 18px;
      text-align: center;
      font-weight: 400;
      color: #ff590f;
      margin-top: 8px;
      line-height: 18px;
    }

    .pic-list {
      width: 248px;
      margin-left: 20px;
      margin-top: 15px;
      display: flex;
      align-items: center;

      .pic-item {
        display: inline-block;
        width: 34px;
        height: 34px;
        border: 1px solid #dddddd;
        margin-right: 8px;
      }

      .pic-item.active {
        border: 1px solid #ff590f;
        text-align: center;
      }

      .pic-item.dot {
        margin-right: 0;
        line-height: 34px;
        text-align: center;
      }
    }

    .bottom {
      width: 248px;
      margin-left: 20px;
      margin-top: 16px;
      display: flex;
      align-items: center;

      .number {
        display: inline-flex;
        align-items: center;

        .minus,
        .plus {
          width: 34px;
          height: 34px;
          border: 1px solid #dddddd;
          text-align: center;
          display: inline-block;
          line-height: 34px;
        }

        .total {
          height: 34px;
          width: 40px;
          line-height: 40px;
          border-top: 1px solid #dddddd;
          border-bottom: 1px solid #dddddd;
          text-align: center;
          display: inline-block;
          font-size: 14px;
          font-weight: 500;
          color: #17183b;
        }

        .disable {
          background-color: #dddddd;
        }
      }

      .button {
        width: 118px;
        height: 34px;
        line-height: 34px;
        background: #535353;
        text-align: center;
        font-size: 14px;
        font-weight: 500;
        color: #ffffff;
        display: inline-block;
        margin-left: 18px;
      }
    }
  }
}
.category {
  box-shadow: 0px -2px 0px 0px #ff590f;
  border: 1px solid #dddddd;
  width: 1200px;
  margin: 0 auto;
  margin-top: 56px;

  .item {
    border-bottom: 1px solid #dddddd;

    display: flex;
    justify-content: top;

    .title {
      font-size: 14px;
      font-weight: 400;
      color: #535353;
      margin-left: 20px;
      display: inline-block;
      width: 170px;
      margin-top: 20px;
      margin-right: 20px;
    }
    .options {
      display: inline-block;
      width: 1000px;
      margin-top: 20px;
      margin-bottom: 20px;
      .option {
        display: inline-block;
        font-size: 14px;
        font-weight: 400;
        color: #222222;
        width: 80px;
        margin-right: 39px;
      }
      .active {
        color: #ff590f;
      }
    }
  }
  .item:last-child {
    border-bottom: none;
  }
}
.list2 {
  border-top: 1px solid #dddddd;
  margin-top: 20px;

  .item:hover {
    .pic {
      border: 2px solid #ff590f;
    }

    .button {
      background-color: #ff590f;
    }
  }

  .item {
    height: 204px;
    border-bottom: 1px solid #dddddd;
    position: relative;

    .pic {
      width: 164px;
      height: 164px;
      background: #ffffff;
      border: 1px solid #dddddd;
      position: relative;
      top: 20px;
      display: flex;
      align-items: center;
      justify-content: center;

      img {
        width: 136px;
        height: 156px;
        background-color: white;
      }
    }

    .name {
      font-size: 16px;
      font-weight: 600;
      color: #222222;
      position: absolute;
      top: 24px;
      left: 212px;
      line-height: 1;
    }

    .attrs {
      position: absolute;
      top: 52px;
      left: 212px;
      max-height: 80px;
      overflow-y: hidden;

      .attrs-item {
        font-size: 14px;
        font-weight: 400;
        color: #535353;
        line-height: 20px;
      }
    }

    .pic-list {
      height: 34px;
      position: absolute;
      left: 212px;
      bottom: 20px;

      .pic-item {
        width: 34px;
        height: 34px;
        border: 1px solid #dddddd;
        margin-right: 8px;
      }

      .pic-item.active {
        border: 1px solid #ff590f;
      }
    }

    .price {
      font-size: 18px;
      font-weight: 400;
      color: #222222;
      line-height: 18px;
      position: absolute;
      top: 93px;
      left: 640px;
    }

    .number {
      display: inline-flex;
      align-items: center;
      position: absolute;
      top: 81px;
      left: 790px;

      .minus,
      .plus {
        width: 42px;
        height: 42px;
        border: 1px solid #dddddd;
        text-align: center;
        display: inline-block;
        line-height: 38px;
        font-size: 24px;
      }

      .total {
        height: 42px;
        width: 64px;
        line-height: 40px;
        border-top: 1px solid #dddddd;
        border-bottom: 1px solid #dddddd;
        text-align: center;
        display: inline-block;
        font-size: 16px;
        font-weight: 500;
        color: #17183b;
      }

      .disable {
        background-color: #dddddd;
      }
    }

    .button {
      width: 164px;
      height: 42px;
      background: #535353;
      line-height: 42px;
      text-align: center;
      position: absolute;
      color: #ffffff;
      top: 81px;
      right: 0;
      font-size: 16px;
      font-weight: 500;
    }
  }
}
</style>
